package com.project.server.broadroom;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.model.LoginUser;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.SecurityUtils;
import com.project.common.utils.poi.ExcelUtil;
import com.project.server.domain.broadroom.TBroadroomBook;
import com.project.server.service.broadroom.ITBroadroomBookService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会议室预约Controller
 *
 * @author project
 * @date 2021-06-22
 */
@RestController
@RequestMapping("/server/book")
public class TBroadroomBookController extends BaseController {

    @Resource
    private ITBroadroomBookService tBroadroomBookService;

    /**
     * 查询会议室预约列表
     */
    @PreAuthorize("@ss.hasPermi('server:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBroadroomBook tBroadroomBook) {
        startPage();
        List<TBroadroomBook> list = tBroadroomBookService.selectTBroadroomBookList(tBroadroomBook);
        return getDataTable(list);
    }

    /**
     * 导出会议室预约列表
     */
    @PreAuthorize("@ss.hasPermi('server:book:export')")
    @Log(title = "会议室预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBroadroomBook tBroadroomBook) {
        List<TBroadroomBook> list = tBroadroomBookService.selectTBroadroomBookList(tBroadroomBook);
        ExcelUtil<TBroadroomBook> util = new ExcelUtil<TBroadroomBook>(TBroadroomBook.class);
        return util.exportExcel(list, "会议室预约记录表");
    }

    /**
     * 获取会议室预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('server:book:query')")
    @GetMapping(value = "/{broadroomBookId}")
    public AjaxResult getInfo(@PathVariable("broadroomBookId") Long broadroomBookId) {
        return AjaxResult.success(tBroadroomBookService.selectTBroadroomBookById(broadroomBookId));
    }

    /**
     * 通过预约时间段查询会议室预约列表
     */
    @PreAuthorize("@ss.hasPermi('server:book:list')")
    @GetMapping("/")
    public AjaxResult getBookList(TBroadroomBook tBroadroomBook) {
        return AjaxResult.success(tBroadroomBookService.selectTBroadroomBookBybookStart(tBroadroomBook));
    }

    /**
     * 新增会议室预约
     */
    @PreAuthorize("@ss.hasPermi('server:book:add')")
    @Log(title = "会议室预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TBroadroomBook tBroadroomBook) {
        return toAjax(tBroadroomBookService.insertTBroadroomBook(tBroadroomBook));

    }

    /**
     * 修改会议室预约
     */
    @PreAuthorize("@ss.hasPermi('server:book:edit')")
    @Log(title = "会议室预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TBroadroomBook tBroadroomBook) {
        LoginUser user = (LoginUser)SecurityUtils.getLoginUser();
        tBroadroomBook.setApproverId(user.getUser().getUserId());
        tBroadroomBook.setApproverName(user.getUsername());
        return AjaxResult.success(tBroadroomBookService.updateTBroadroomBook(tBroadroomBook));
    }

    /**
     * 删除会议室预约
     */
    @PreAuthorize("@ss.hasPermi('server:book:remove')")
    @Log(title = "会议室预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{broadroomBookIds}")
    public AjaxResult remove(@PathVariable Long[] broadroomBookIds) {
        return toAjax(tBroadroomBookService.deleteTBroadroomBookByIds(broadroomBookIds));
    }
}
